﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TrainingWorkshop.IDAL;

namespace TrainingWorkshop.DAL_EF
{
    public class CourseListRepository_EF : ICourseListRepository
    {


        /// <summary>
        /// Select All Courses in Given Condition
        /// </summary>
        /// <param name="condition">Given Condition</param>
        /// <returns>List of Course</returns>
        public List<Model.CourseList> SelectAllCourses(Model.Condition condition)
        {
            using (var db = new projectbEntities())
            {
                
                 bool status = false;
                if (condition.CourseStatus >= 0)
                {
                   
                    if (condition.CourseStatus == 1)
                    {
                        status = true;
                    }
                }
                //join tables
                IQueryable<Model.CourseList> courseList = from c in db.Courses
                                                     join cd in db.CourseDetails
                                                     on c.CourseId equals cd.CourseId
                                                     join l in db.Levels
                                                     on c.LevelId equals l.LevelId
                                                     join t in db.Teachers
                                                     on c.TeacherId equals t.TeacherId

                                                     where ((condition.TeacherName.Length == 0) || (t.TeacherName == condition.TeacherName))
                                                     && ((condition.LevelName.Length == 0) || (l.LevelName == condition.LevelName))
                                                     &&((condition.CourseName.Length==0)||(c.CourseName.Contains(condition.CourseName)))
                                                     &&((condition.CourseStatus<0)||(c.CourseStatus==status))

                                                     group c by new
                                                     {
                                                         c.CourseId,
                                                         c.CourseName,
                                                         c.CourseStatus,
                                                         t.TeacherName,
                                                         l.LevelName

                                                     } into cl

                                                     select new TrainingWorkshop.Model.CourseList
                                                     {
                                                         CourseId = cl.Key.CourseId,
                                                         CourseName = cl.Key.CourseName,
                                                         CourseStatus = cl.Key.CourseStatus,
                                                         TeacherName = cl.Key.TeacherName,
                                                         LevelName = cl.Key.LevelName,
                                                         StudentNumber = cl.Count()
                                                     };
                int i=courseList.Count();
                return courseList.ToList();
            }
        }
    }
}